package com.xtaller.easy.admin.config.interceptor;


import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.bean.AuthInfo;
import com.xtaller.common.bean.CacheInfo;
import com.xtaller.common.bean.JWTResult;
import com.xtaller.common.convert.J;
import com.xtaller.common.convert.V;
import com.xtaller.common.kit.JwtKit;
import com.xtaller.easy.admin.base.BaseInterceptor;
import com.xtaller.easy.admin.tps.CacheKit;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Taller on 2018/3/13
 * 第一层数据拦截 token基础认证
 */
@Component
@Order(1)
public class TokenInterceptor extends BaseInterceptor {
    @Autowired
    public CacheKit cacheKit;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String token = request.getHeader("token");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        // 处理404
        if (V.isEqual(request.getServletPath(), "/error")){
            response.setStatus(404);
            response.getWriter().write(J.s2r("找不到对应的路由"));
            return false;
        }
        // token判断
        if(V.isEmpty(token)){
            response.setStatus(401);
            response.getWriter().write(J.s2r("token 口令不能为空 !"));
            return false;
        }

        // Token有效认证
        // 一个token只能在一个设备上
        JWTResult jwt = JwtKit.validateJWT(token);
        if (jwt.isSuccess()) {
            Claims claims = jwt.getClaims();
            AuthInfo auth = J.s2m(claims.getSubject(), AuthInfo.class);
            auth.setId(claims.getId());
            CacheInfo info = cacheKit.getCacheInfo(auth.getId());
            JSONObject object = null;
            try{
                object = info.checkJwt(auth.getPlatform(), token);
            }catch (Exception ex){
                response.setStatus(403);
                response.getWriter().write(J.s2r("签名认证过期,请重新登录", 1));
                return false;
            }
            if(!object.getBoolean("check")){
                response.setStatus(403);
                response.getWriter().write(J.s2r(object.getString("message"), 1));
                return false;
            }
        }else{
            response.setStatus(403);
            response.getWriter().write(J.s2r("签名信息异常!", 1));
            return false;
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
